import logging
import random, time
from datetime import datetime
from odoo import api, models
_logger = logging.getLogger(__name__)

class RtxDtuDataService(models.AbstractModel):
    _inherit = 'dtu.data.service'

    def start_service_demo(self):
        explain_obj = self.env['dtu.data']
        project_obj = self.env['project.project']
        location_obj = self.env['project.map.location']
        imei_records = [
            (['新疆供水线路1', '863482067865689', '44.3205343504481', '89.3099584704077'],
            ['新疆供水线路1', '863482067865690', '44.3829801941349', '89.3369683715185'],
            ['新疆供水线路1', '863482067865691', '44.4279297423072', '89.3370304367216'],
            ['新疆供水线路1', '863482067865692', '44.4600190759255', '89.3624319692118'],
            ['新疆供水线路1', '863482067865693', '44.4891646439968', '89.4075003574451'],
            ['新疆供水线路1', '863482067865694', '44.508953685894', '89.4534074353228'],
            ['新疆供水线路1', '863482067865695', '44.5246668646336', '89.4854102279479'],
            ['新疆供水线路1', '863482067865696', '44.5409530111931', '89.5198969358647']),

            (['新疆供水线路2', '863482067865697', '44.7906813064407', '88.7326015255042'],
            ['新疆供水线路2', '863482067865698', '44.8119298138776', '88.6545492627515'],
            ['新疆供水线路2', '863482067865699', '44.810444399436', '88.5975188239167'],
            ['新疆供水线路2', '863482067865700', '44.8116773177872', '88.4954363487061'],
            ['新疆供水线路2', '863482067865701', '44.8306806538559', '88.4302574901839'],
            ['新疆供水线路2', '863482067865702', '44.8788003799357', '88.3758352326556'],
            ['新疆供水线路2', '863482067865703', '44.8963595089817', '88.3185370724696'],
            ['新疆供水线路2', '863482067865704', '44.9066497775628', '88.2271219650494'],
            ['新疆供水线路2', '863482067865705', '44.9083299424051', '88.1317009758892'],
            ['新疆供水线路2', '863482067865706', '44.8987297964667', '88.0644999230135']),

            (['新疆供水线路3', '863482067865707', '44.7889903963706', '88.7191690179985'],
            ['新疆供水线路3', '863482067865708', '44.8032092067848', '88.5984825794933'],
            ['新疆供水线路3', '863482067865709', '44.8173185444187', '88.4581635156102'],
            ['新疆供水线路3', '863482067865710', '44.8312546081822', '88.3177840796085'],
            ['新疆供水线路3', '863482067865711', '44.8357504899088', '88.1740973895832'],
            ['新疆供水线路3', '863482067865712', '44.8560284737808', '87.9518672291614'],
            ['新疆供水线路3', '863482067865713', '44.8715518289764', '87.7883105212732'],
            ['新疆供水线路3', '863482067865714', '44.903080206733', '87.6276756212078']),

            (['新疆供水线路4', '863482067865715', '44.7843898445661', '88.709350000627'],
            ['新疆供水线路4', '863482067865716', '44.7755670664391', '88.5168078896571'],
            ['新疆供水线路4', '863482067865717', '44.7733675869052', '88.3373916100577'],
            ['新疆供水线路4', '863482067865718', '44.7547301521471', '88.1939140860125'],
            ['新疆供水线路4', '863482067865719', '44.7544183432007', '88.0438974559874'],
            ['新疆供水线路4', '863482067865720', '44.7492213734613', '87.8808463202668'],
            ['新疆供水线路4', '863482067865721', '44.7255238302883', '87.7669006419487']),

            (['克拉玛依线路1', '863482067865725', '45.79889716780939', '85.85718620748438'],
             ['克拉玛依线路1', '863482067865726', '45.863971259368896', '85.88653851969046'],
             ['克拉玛依线路1', '863482067865727', '45.94010414528817', '85.91055404785911'],
             ['克拉玛依线路1', '863482067865728', '45.997598727201115', '85.98526902438374'],
             ['克拉玛依线路1', '863482067865729', '46.06984585624085', '86.04397364879596'],
             ['克拉玛依线路1', '863482067865730', '46.119807773904256', '86.1560461135829'],
             ['克拉玛依线路1', '863482067865731', '46.11610844337735', '86.2627817943324'],
             ['克拉玛依线路1', '863482067865732', '46.119807773904256', '86.36684908306313'],
             ['克拉玛依线路1', '863482067865733', '46.11240886449312', '86.45757441170018']),

            (['克拉玛依线路2', '863482067865735', '45.83228671073449', '85.87760429748123'],
             ['克拉玛依线路2', '863482067865736', '45.93259466309914', '85.88827786555619'],
             ['克拉玛依线路2', '863482067865737', '46.11047278036806', '85.91496178574356'],
             ['克拉玛依线路2', '863482067865738', '46.195503251592925', '85.95765605804336'],
             ['克拉玛依线路2', '863482067865739', '46.35043732833466', '86.0483813866804'],
             ['克拉玛依线路2', '863482067865740', '46.45716001937508', '86.12843314724252'],
             ['克拉玛依线路2', '863482067865741', '46.6076876317514', '86.20848490780462'],
             ['克拉玛依线路2', '863482067865742', '46.699268026149475', '86.34190450874146']),

            (['克拉玛依线路3', '863482067865745', '45.82856812332999', '85.9042882176686'],
             ['克拉玛依线路3', '863482067865746', '45.91774565325212', '86.12843314724252'],
             ['克拉玛依线路3', '863482067865747', '45.9474396983182', '86.26185274817935'],
             ['克拉玛依线路3', '863482067865748', '45.9919509568891', '86.44330340545348'],
             ['克拉玛依线路3', '863482067865749', '45.9919509568891', '86.61408049465264']),

            (['克拉玛依线路4', '863482067865755', '45.74408403818688', '85.890511054747'],
             ['克拉玛依线路4', '863482067865756', '45.65321517568789', '85.84291199313252'],
             ['克拉玛依线路4', '863482067865757', '45.57774849921057', '85.83021891003534'],
             ['克拉玛依线路4', '863482067865758', '45.519970160120955', '85.85877834700402'],
             ['克拉玛依线路4', '863482067865759', '45.44877682408978', '85.8968575962956'],
             ['克拉玛依线路4', '863482067865760', '45.43096443206845', '86.04917459346194']),
        ]
        for imeis in imei_records:
            for index, imei in enumerate(imeis):
                traffic = random.randint(200, 300),  # 流量
                if index == 0:
                    # 写入管道第一个数据到泵站
                    location_id = location_obj.search([('imei', '=', f'{imei[1]}')])
                    location_id.write({'real_input_traffic': traffic[0]})
                    # 写入管道第一个数据到项目
                    project_id = project_obj.search([('name', '=', f'{imei[0]}')])
                    project_id.write({'real_input_traffic': traffic[0]})
                if index == len(imeis) - 1:
                    # 写入管道最后一个数据到泵站
                    location_id = location_obj.search([('imei', '=', f'{imei[1]}')])
                    location_id.write({'real_output_traffic': traffic[0]})
                    # 写入管道最后一个数据到项目
                    project_id = project_obj.search([('name', '=', f'{imei[0]}')])
                    project_id.write({'real_output_traffic': traffic[0]})
                pv = {
                    "name": imei[0],
                    "imei": imei[1],
                    "time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                    "model": False,
                    "version": False,
                    "running_time": False,
                    "remaining_memory": False,
                    "signal_strength": False,
                    "pressure": random.randint(90, 105),     # 压力
                    "traffic": traffic[0],     # 流量
                    "liquid_level": random.randint(50, 70),     # 液位
                    "temperature": random.randint(30, 55),     # 温度
                    "atmospheric_pressure": random.randint(10, 25),     # 气压
                    "humidity": random.randint(40, 70),     # 湿度
                    "gps_longitude": False,
                    "gps_latitude": False,
                    "gsm_longitude": imei[2],
                    "gsm_latitude": imei[3],
                }
                _logger.info(f"{pv}")
                try:
                    # 直接使用 pv 字典创建记录
                    record = explain_obj.create(pv)
                    if record:
                        _logger.info("Data created successfully.")
                        # 手动提交事务，确保数据实时写入
                        self.env.cr.commit()
                    else:
                        _logger.error("Failed to create data.")
                except Exception as e:
                    _logger.error(f"Error creating data: {e}")
                    # 回滚事务，防止数据不一致
                    self.env.cr.rollback()
                time.sleep(3)


